import { BaseWindow } from "../../../../component/window/BaseWindow";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { Language } from "../../../../../extend/language/Language";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { EffectManager } from "../../../manager/EffectManager";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { PetEventName } from "../PetEventName";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { EBind } from "../../../../../Message/Public/EBind";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { RefreshConfirmWindow } from "./RefreshConfirmWindow";
import { Cache } from "../../../cache/Cache";
import { SkillConfig } from "../../../resource/SkillConfig";
import { PetController } from "../../../control/PetController";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { ItemData } from "../../../resource/info/ItemData";
import { GuideTipsManager } from "../../../manager/GuideTipsManager";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { TSkillItem } from "./TSkillItem";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GComboBox } from "../../../../../com/mui/controls/GComboBox";
import { MailFriendItemCellRender } from "../../mail/MailFriendItemCellRender";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { StarPanel } from "./StarPanel";
import { PetMingMsgRollTips } from "../ming/PetMingMsgRollTips";
import { TSkill } from "../../../../../Message/Db/Tables/TSkill";
import { PetSkillUtil } from "./PetSkillUtil";
import { _needCost } from "../../../../../this/_needCost";
import { _itemData } from "../../../../../this/_itemData";
import { _skills } from "../../../../../this/_skills";
type int = number;
//class PetSkillRefreshWindow
    
    export  class PetSkillRefreshWindow extends BaseWindow
    {
        constructor()
        {
            this._costDp = new DataProvider([{"id": 2, "label": _needCost + Language.getString(69905)}, {"id": 3, "label": _needCost + Language.getString(69907)}]);
            super();
            this.init();
            this.registToolTip();
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onItemdataChangeHandler);
            return;
        }

        /* internal  */onUnitChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._costUnit = this._costCombo.selectedItem.id;
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var btn: GButton;
            var unit: int;
            var onFreeOkClickHandler: Function;
            var onCostAlertHandler: Function;
            var onGetOkClickHandler: Function;

            var loc1;
            e = arg1;
            btn = e.target as GButton;
            unit = this._costCombo.selectedItem.id;
            if (this._itemData) 
            {
                if (btn.name != "free") 
                {
                    if (btn.name != "refresh") 
                    {
                        if (btn.name != "batchRefresh") 
                        {
                            if (btn.name == "getSkill") 
                            {
                                EffectManager.glowFilterUnReg(btn);
                                if (this._skill) 
                                {
                                    onGetOkClickHandler = (arg1: int): void
                                    {
                                        if (arg1 == Alert.OK) 
                                        {
                                            Dispatcher.dispatchEvent(new DataEvent(EventName.PetMakeSkillProp, {"uid": _itemData.uid, "skillId": _skill.skillId}));
                                        }
                                        return;
                                    }
                                    Alert.show(Language.getStringByParam(54612, this._colorSkillName), null, Alert.OK | Alert.CANCEL, null, onGetOkClickHandler);
                                }
                            }
                        }
                        else 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(PetEventName.PetBatchRefreshWindow, {"itemData": _itemData, "skills": _skills, "allTimes": this.allTimes}));
                        }
                    }
                    else 
                    {
                        EffectManager.glowFilterUnReg(btn);
                        if (this._costUnit == EPrictUnit._EPriceUnitGoldBind && this._itemData.itemInfo.item.bind == EBind._EBindNo) 
                        {
                            onCostAlertHandler = (arg1: int): void
                            {
                                if (arg1 == Alert.OK) 
                                {
                                    runRefresh(false);
                                }
                                return;
                            }
                            Alert.show(Language.getString(54714), null, Alert.OK | Alert.CANCEL, null, onCostAlertHandler);
                        }
                        else 
                        {
                            this.runRefresh(false);
                        }
                    }
                }
                else 
                {
                    EffectManager.glowFilterUnReg(btn);
                    if (this._itemData.itemInfo.item.bind != EBind._EBindNo) 
                    {
                        this.runRefresh(true);
                    }
                    else 
                    {
                        onFreeOkClickHandler = (arg1: int): void
                        {
                            if (arg1 == Alert.OK) 
                            {
                                runRefresh(true);
                            }
                            return;
                        }
                        Alert.show(Language.getString(54442), null, Alert.OK | Alert.CANCEL, null, onFreeOkClickHandler);
                    }
                }
            }
            return;
        }

        /* internal  */runRefresh(arg1: Boolean=false): void
        {
            var isFreeRand: Boolean=false;
            var isNotNeedAlert: Boolean;
            var clickHandler: Function;

            var loc1;
            isFreeRand = arg1;
            isNotNeedAlert = PulseSharedObject.isTodayNotTips(RefreshConfirmWindow.confirmKey);
            if (isNotNeedAlert) 
            {
                this.dealAlertHighSkill(isFreeRand);
            }
            else if (isFreeRand) 
            {
                this.dealAlertHighSkill(true);
            }
            else if (Cache.instance.guidePetSkillRefresh.needSkillRefreshPanel()) 
            {
                this.dealAlertHighSkill();
            }
            else 
            {
                clickHandler = (arg1: Boolean): void
                {
                    if (arg1) 
                    {
                        dealAlertHighSkill();
                    }
                    _confirmWindow.hide();
                    return;
                }
                if (this._confirmWindow == null) 
                {
                    this._confirmWindow = new RefreshConfirmWindow();
                }
                this._confirmWindow.updateData(this._costUnit, this._needCost, 1, clickHandler);
                this._confirmWindow.show();
            }
            return;
        }

        /* internal  */isContainHighSkill(): Boolean
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._skills.length) 
            {
                loc1 = SkillConfig.instance.getInfoByName(this._skills[loc2] as int);
                if (loc1 && loc1.skillLevel == 4) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        /* internal  */dealAlertHighSkill(arg1: Boolean=false): void
        {
            var isFreeRand: Boolean=false;
            var unit: int;
            var refreshTime: int;
            var onGoOnClickHandler: Function;

            var loc1;
            unit = 0;
            refreshTime = 0;
            onGoOnClickHandler = null;
            isFreeRand = arg1;
            onGoOnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 == Alert.OK) 
                {
                    PetController.isBatchRefresh = false;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetRandSkillProp, {"uid": _itemData.uid, "unit": unit, "times": refreshTime}));
                    Alert.resetToDefault();
                }
                _isNeedAlert = !arg2;
                return;
            }
            unit = this._costCombo.selectedItem.id;
            if (isFreeRand) 
            {
                refreshTime = 0;
            }
            else 
            {
                refreshTime = 1;
            }
            if (this._isNeedAlert && this.isContainHighSkill()) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.checkBoxLabel = Language.getString(54160);
                Alert.show(Language.getString(54161), null, Alert.OK | Alert.CANCEL, null, onGoOnClickHandler);
            }
            else 
            {
                PetController.isBatchRefresh = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetRandSkillProp, {"uid": this._itemData.uid, "unit": unit, "times": refreshTime}));
            }
            return;
        }

        public get itemData(): ItemData
        {
            return this._itemData;
        }

        public set itemData(arg1: ItemData)
        {
            this._itemData = arg1;
            if (Cache.instance.guidePetSkillRefresh.needSkillRefreshPanel()) 
            {
                if (Cache.instance.guidePetSkillRefresh.isGuideItem(this.itemData)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetSkillWindowShow, this.itemData));
                }
                else 
                {
                    this.clearGuide();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetSkillWindowClose, this.itemData));
                }
            }
            return;
        }

        public get skills(): Array<any>
        {
            return this._skills;
        }

        public set skills(arg1: Array<any>)
        {
            this._skills = arg1;
            if (arg1.length > 0) 
            {
                this._skill = SkillConfig.instance.getInfoByName(arg1[(arg1.length - 1)] as int);
                this.updateByTSKill(this._skill);
                this._tskillItem.skill = this._skill;
            }
            return;
        }

        public get allTimes(): int
        {
            return this._allTimes;
        }

        public set allTimes(arg1: int)
        {
            this._allTimes = arg1;
            this._starPanel.num = arg1;
            return;
        }

        public clear(): void
        {
            this._itemData = null;
            this.skills = [];
            this._tskillItem.skill = null;
            this.allTimes = 0;
            this.clearGuide();
            return;
        }

        /* internal  */clearGuide(): void
        {
            this._freeBtn.enabled = true;
            this._refreshBtn.enabled = true;
            this._getBtn.enabled = true;
            this._batchBtn.enabled = true;
            EffectManager.glowFilterUnReg(this._freeBtn);
            EffectManager.glowFilterUnReg(this._refreshBtn);
            EffectManager.glowFilterUnReg(this._getBtn);
            EffectManager.glowFilterUnReg(this._batchBtn);
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetSkillWindowShow, this.itemData));
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.clearGuide();
            this._isNeedAlert = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetSkillWindowClose, this.itemData));
            return;
        }

        public guideRefreshSkill(): void
        {
            var loc1=null;
            if (Cache.instance.guidePetSkillRefresh.isGuideItem(this.itemData)) 
            {
                loc1 = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_BR, Language.getString(21053));
                loc1.x = this._freeBtn.x + 10;
                loc1.y = this._freeBtn.y;
                addChild(loc1);
                EffectManager.glowFilterReg(this._freeBtn, null);
                this._freeBtn.enabled = true;
                this._refreshBtn.enabled = false;
                this._getBtn.enabled = false;
                this._batchBtn.enabled = false;
            }
            return;
        }

        public guideRefreshSkill2(): void
        {
            var loc1=null;
            if (Cache.instance.guidePetSkillRefresh.isGuideItem(this.itemData)) 
            {
                loc1 = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_BL, Language.getString(21053));
                loc1.x = this._refreshBtn.x + this._refreshBtn.width - 10;
                loc1.y = this._refreshBtn.y;
                addChild(loc1);
                EffectManager.glowFilterReg(this._refreshBtn, null);
                this._freeBtn.enabled = false;
                this._refreshBtn.enabled = true;
                this._getBtn.enabled = false;
                this._batchBtn.enabled = false;
            }
            return;
        }

        /* internal  */registToolTip(): void
        {
            this._refreshBtn.toolTipData = HTMLUtil.addColor(Language.getString(54157), "#ffffff");
            this._batchBtn.toolTipData = HTMLUtil.addColor(Language.getString(54158), "#ffffff");
            this._getBtn.toolTipData = HTMLUtil.addColor(Language.getString(54159), "#ffffff");
            return;
        }

        public guideCallSkill(): void
        {
            var loc1=null;
            if (Cache.instance.guidePetSkillRefresh.isGuideItem(this.itemData)) 
            {
                if (this._getBtn && this._getBtn.visible) 
                {
                    loc1 = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_BL, Language.getString(21054));
                    loc1.x = this._getBtn.x + this._getBtn.width - 10;
                    loc1.y = this._getBtn.y;
                    addChild(loc1);
                    EffectManager.glowFilterReg(this._getBtn, null);
                    this._getBtn.enabled = true;
                    this._freeBtn.enabled = false;
                    this._refreshBtn.enabled = false;
                    this._batchBtn.enabled = false;
                }
            }
            return;
        }

        /* internal  */onItemdataChangeHandler(arg1: Object=null): void
        {
            if (this.isHide == false) 
            {
                if (this._itemData) 
                {
                    this._itemData = Cache.instance.pack.backPackCache.getPropItemDataByUid(this._itemData.uid);
                }
            }
            return;
        }

        public showMsg(arg1: string): void
        {
            this._msgRollTips.showMsg(arg1, 1);
            return;
        }

        /* internal  */init(): void
        {
            this.setSize(250, 305);
            this.titleHeight = 28;
            this.title = Language.getString(54147);
            ObjCreate.createBg(17, 39, 215, 180, this);
            ObjCreate.createBg(95, 60, 68, 68, this, ImagesConst.PackItemBg);
            this._tskillItem = new TSkillItem();
            this._tskillItem.setSize(60, 60);
            this._tskillItem.move(99, 64);
            this.addChild(this._tskillItem);
            this._nameTxt = ObjCreate.createTextField("", 0, 138, 250, 45, this, new GTextFormat(FontUtil.songtiName, 16, 16711935, true));
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._costCombo = new GComboBox();
            this._costCombo.move(37, 167);
            this._costCombo.width = 100;
            this._costCombo.height = 20;
            this._costCombo.buttonMode = true;
            this._costCombo.styleName = "GComboboxStyle";
            this._costCombo.drawNow();
            this._costCombo.dropdown.setStyle("cellRenderer", MailFriendItemCellRender);
            this._costCombo.dropdown.setStyle("skin", ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB));
            this._costCombo.textField.setStyle("textFormat", new GTextFormat("", 12, 11661308, false, false, false, "", "", flash.text.TextFormatAlign.CENTER));
            this._costCombo.dataProvider = this._costDp;
            this._costCombo.addEventListener(flash.events.Event.CHANGE, this.onUnitChangeHandler);
            this._costCombo.selectedIndex = 0;
            addChild(this._costCombo);
            this._freeBtn = ObjCreate.createGButton(Language.getString(54629), 37, 167, 70, 24, this);
            this._freeBtn.name = "free";
            this._freeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._refreshBtn = ObjCreate.createGButton(Language.getString(54154), 145, 168, 70, 24, this);
            this._refreshBtn.name = "refresh";
            this._batchBtn = ObjCreate.createGButton(Language.getString(54155), 45, 225, 70, 24, this);
            this._batchBtn.name = "batchRefresh";
            this._getBtn = ObjCreate.createGButton(Language.getString(54156), 135, 225, 70, 24, this);
            this._getBtn.name = "getSkill";
            this._refreshBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._batchBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._getBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._tipTxt = ObjCreate.createTextField("", 0, 197, 250, 20, this, GlobalStyle.textFormat6);
            this._tipTxt.text = Language.getString(54441);
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._starPanel = new StarPanel();
            this._starPanel.x = 25;
            this._starPanel.y = 260;
            this.addChild(this._starPanel);
            this._msgRollTips = new PetMingMsgRollTips();
            this._msgRollTips.x = 37;
            this._msgRollTips.y = 167;
            this.addChild(this._msgRollTips);
            return;
        }

        public set isCanFreeRand(arg1: Boolean)
        {
            this._isCanFreeRand = arg1;
            if (arg1) 
            {
                this._freeBtn.visible = true;
                this._costCombo.visible = false;
            }
            else 
            {
                this._freeBtn.visible = false;
                this._costCombo.visible = true;
            }
            return;
        }

        /* internal  */updateByTSKill(arg1: TSkill): void
        {
            if (arg1) 
            {
                this._colorSkillName = HTMLUtil.addColor(arg1.skillName, PetSkillUtil.getColorByLevel(arg1.skillLevel));
                this._nameTxt.htmlText = this._colorSkillName;
            }
            return;
        }

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costCombo: GComboBox;

        /* internal */ /* var */_refreshBtn: GButton;

        /* internal */ /* var */_batchBtn: GButton;

        /* internal */ /* var */_getBtn: GButton;

        /* internal */ /* var */_starPanel: StarPanel;

        /* internal */ /* var */_tskillItem: TSkillItem;

        /* internal */ /* var */_confirmWindow: RefreshConfirmWindow;

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */_skills: Array<any>;

        /* internal */ /* var */_allTimes: int;

        /* internal */ /* var */_skill: TSkill;

        /* internal */ /* var */_needCost: int=10;

        /* internal */ /* var */_costUnit: int=2;

        /* internal */ /* var */_isNeedAlert: Boolean=true;

        /* internal */ /* var */_colorSkillName: string;

        /* internal */ /* var */_msgRollTips: PetMingMsgRollTips;

        /* internal */ /* var */_costDp: DataProvider;

        /* internal */ /* var */_freeBtn: GButton;

        /* internal */ /* var */_isCanFreeRand: Boolean;
    }
